package com.hmh.student.system.controller;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.system.entity.SysUser;
import com.hmh.student.system.entity.SysUserRole;
import com.hmh.student.system.service.ISysRoleService;
import com.hmh.student.system.service.ISysUserRoleService;
import com.hmh.student.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-09
 */
@RestController
@RequestMapping("/sysUserRole")
@Api(tags = "用户角色管理")
public class SysUserRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysRoleService sysRoleService;


    //添加
    @PostMapping("/addSysUserRole")
    public Map<String, Object> addSysUserRole(@RequestBody SysUserRole sysUserRole) {
        Map<String, Object> map = new HashMap<>();

        // 1. 校验 roleId 是否存在
        if (sysUserRole.getRoleId() == null || !sysRoleService.existsById(sysUserRole.getRoleId())) {
            map.put("msg", "fail");
            map.put("error", "角色ID不存在或未提供");
            return map;
        }

        // 2. 保存用户角色关系
        try {
            sysUserRoleService.save(sysUserRole);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            map.put("error", "新增失败：" + e.getMessage());
        }
        return map;
    }


    // 获取单个用户角色信息
    @GetMapping("/{id}")
    public ResponseEntity<SysUserRole> getById(@PathVariable Integer id) {
        SysUserRole userRole = sysUserRoleService.getById(id);
        if (userRole == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userRole);
    }


    //修改
    // 修改用户角色（校验 roleId 是否存在）
    @PutMapping("/updateSysUserRole")
    public Map<String, Object> updateSysUserRole(@RequestBody SysUserRole sysUserRole) {
        Map<String, Object> map = new HashMap<>();

        // 1. 校验 roleId 是否存在
        if (sysUserRole.getRoleId() == null || !sysRoleService.existsById(sysUserRole.getRoleId())) {
            map.put("msg", "fail");
            map.put("error", "角色ID不存在或未提供");
            return map;
        }

        // 2. 更新用户角色关系
        try {
            sysUserRoleService.updateById(sysUserRole);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            map.put("error", "更新失败：" + e.getMessage());
        }
        return map;
    }


    //删除
    @DeleteMapping("/deleteSysUserRole")
    public Map<String,Object> deleteSysUserRole(@RequestParam Integer id) {
        Map<String,Object> map = new HashMap<>();
        try {
            sysUserRoleService.removeById(id);
            map.put("msg","success");

        }catch (Exception e) {
            map.put("msg","fail");
            map.put("msg","删除失败"+e.getMessage());
        }
        return map;

    }

    //分页查询
    @GetMapping("/pageSysUserRole")
    public Page<SysUserRole> pageSysUserRole(@RequestParam(required = false) Integer userId,
                                             @RequestParam(required = false) Integer roleId,
                                             @RequestParam(defaultValue = "1") Integer current,
                                             @RequestParam(defaultValue = "10") Integer pageSize) {
         return sysUserRoleService.queryUserRolepage(userId,roleId,current,pageSize);
    }



}
